package cn.yuan.org.controller;


import cn.yuan.basic.util.PageList;
import cn.yuan.org.domain.DictionaryType;
import cn.yuan.org.query.Query;
import cn.yuan.org.service.IDictionaryTypeDetailService;
import cn.yuan.org.service.IDictionaryTypeService;
import cn.yuan.util.AjaxResult.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/dic")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;
    @Autowired
    private IDictionaryTypeDetailService dictionaryTypeDetailService;

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    public PageList<DictionaryType> getDictionaryType(@RequestBody Query query){
        return dictionaryTypeService.getDictionaryType(query);
    }

    /**
     * 增加和修改数据
     * @param dictionaryType
     * @return
     */
    @PutMapping
    public AjaxResult save(@RequestBody DictionaryType dictionaryType){
        try {
            if(dictionaryType.getId() == null){
                dictionaryTypeService.save(dictionaryType);
            }else{
                dictionaryTypeService.update(dictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络错误，请稍后重试！");
        }
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delById(@PathVariable Long id){
        try {
            dictionaryTypeDetailService.delByTypesId(id);
            dictionaryTypeService.delById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络延迟，请稍后重试！");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult delByIds(@RequestBody List<Long> ids){
        try {
            dictionaryTypeService.delByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络延迟，请稍后重试！");
        }
    }
}
